﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Model;
using Common;
using System.IO;

namespace Controller
{
    public class PropertyGridBO
    {
        private PropertyGridDAO _pgDAO;
        public PropertyGridDAO pgDAO
        {
            get
            {
                if (_pgDAO == null)
                    _pgDAO = FactoryModelDAO.CurrentFA.CreatePropertyGridDAOInstance();
                return _pgDAO;
            }
        }



        /// <summary>
        /// 获取控件的缩写名称
        /// </summary>
        /// <param name="doc">配置文件路径</param>
        /// <param name="ControlType">控件的类型（TextBox,Label等）</param>
        /// <param name="ControlClass">控件的种类（WinForm，DevExpress等）</param>
        /// <returns></returns>
        public string GetPrefixOfControl(string ControlType, string ControlClass)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(PubConstant.ConfigPath + PubConstant.ControlPrefixName);
            return ServerExplorerDAO.GetPrefixOfControl(doc, ControlType, ControlClass);
        }



        /// <summary>
        /// 通过属性种类获取控件属性列表
        /// </summary>
        /// <param name="PropertyType">属性的种类</param>
        /// <param name="ControlName">控件名称</param>
        /// <param name="ControlClass">控件的种类（Win,Dev,Web）</param>
        /// <returns></returns>
        public List<eProperty> GetControlTemplateInfoByPropertyType(enumPropertyType PropertyType, string ControlName, string ControlClass)
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                string path = PubConstant.TemplatePath;
                if (String.IsNullOrEmpty(ControlClass) || String.IsNullOrEmpty(ControlName))
                    return new List<eProperty>();
                path = String.Format("{0}{1}-{2}.xml", path, ControlClass, ControlName);
                if (!File.Exists(path))
                {
                    return new List<eProperty>();
                }
                //switch(ControlClass)
                //{
                //    case "WinForm": path = String.Format("Win-{0}{1}", path, ControlName); break;
                //    case "DevExpress": path = String.Format("Dev-{0}{1}", path, ControlName); break;
                //    case "WebForm": path = String.Format("Web-{0}{1}", path, ControlName); break;
                //}

                doc.Load(path);
            }
            catch (Exception ex)
            { throw ex; }
            return pgDAO.GetControlTemplateInfoByPropertyType(doc, PropertyType);
        }



        /// <summary>
        /// 获取控件所有属性列表
        /// </summary>
        /// <param name="ControlName">控件名称</param>
        /// <param name="ControlClass">控件的种类（Win,Dev,Web）</param>
        /// <returns></returns>
        public List<eProperty> GetControlTemplateInfo(string ControlName, string ControlClass)
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                string path = PubConstant.TemplatePath;
                if (String.IsNullOrEmpty(ControlClass) || String.IsNullOrEmpty(ControlName))
                    return new List<eProperty>();
                path = String.Format("{0}{1}-{2}.xml", path, ControlClass, ControlName);
                if (!File.Exists(path))
                {
                    return new List<eProperty>();
                }
                //switch(ControlClass)
                //{
                //    case "WinForm": path = String.Format("Win-{0}{1}", path, ControlName); break;
                //    case "DevExpress": path = String.Format("Dev-{0}{1}", path, ControlName); break;
                //    case "WebForm": path = String.Format("Web-{0}{1}", path, ControlName); break;
                //}

                doc.Load(path);
            }
            catch(Exception ex)
            { throw ex; }
            return pgDAO.GetControlTemplateInfo(doc);

        }



        /// <summary>
        /// 获取文档所有属性列表
        /// </summary>
        /// <returns></returns>
        public List<eProperty> GetDocumentTemplateInfo(enumBaseControlType enumBase)
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                string path = PubConstant.TemplatePath;

                path = String.Format("{0}Document.xml", path);
                if (!File.Exists(path))
                {
                    return new List<eProperty>();
                }
                doc.Load(path);
            }
            catch (Exception ex)
            { throw ex; }
            return pgDAO.GetDocumentTemplateInfo(doc,enumBase);

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="enumBase"></param>
        /// <returns></returns>
        public exFullInfoForBuildChild GetBaseTypeInfoFromTemplate(enumBaseControlType enumBase)
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                string path = PubConstant.TemplatePath;

                path = String.Format("{0}Document.xml", path);
                if (!File.Exists(path))
                {
                    return new exFullInfoForBuildChild();
                }
                doc.Load(path);
            }
            catch (Exception ex)
            { throw ex; }
            return pgDAO.GetBaseTypeInfoFromTemplate(doc,enumBase);

        }

        /// <summary>
        /// 获取布局控件模板
        /// </summary>
        /// <param name="enumLay"></param>
        /// <returns></returns>
        public exFullInfoForBuildChild GetLayoutInfoFromTemplate(enumLayoutTypeSimple enumLay)
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                string path = PubConstant.TemplatePath;

                path = String.Format("{0}LayoutControl.xml", path);
                if (!File.Exists(path))
                {
                    return new exFullInfoForBuildChild();
                }
                doc.Load(path);
                return pgDAO.GetLayoutInfoFromTemplate(doc,enumLay);
            }
            catch (Exception ex)
            {
                
                throw ex;
            }

        }

 
    }
}
